package com.ruoyi.project.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.domain.SysProduct;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.domain.SysUserProductPrice;
import com.ruoyi.project.system.domain.vo.ProductEquity;
import com.ruoyi.project.system.domain.vo.Promote;
import com.ruoyi.project.system.domain.vo.PromoteEquity;
import com.ruoyi.project.system.mapper.SysProductMapper;
import com.ruoyi.project.system.mapper.SysUserProductPriceMapper;
import com.ruoyi.project.system.service.ISysProductService;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 产品Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@Service
public class SysProductServiceImpl implements ISysProductService {

    @Autowired
    private SysProductMapper sysProductMapper;

    @Autowired
    private SysUserProductPriceMapper selectSysUserProductPrice;

    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 得到系统底价
     */
    public BigDecimal getSysUserLevelFloorPrice(SysProduct sysProduct, String level) {
        if ("V0".equals(level)) {
            return sysProduct.getV0Pricing();
        } else if ("V1".equals(level)) {
            return sysProduct.getV1Pricing();
        } else if ("V2".equals(level)) {
            return sysProduct.getV2Pricing();
        } else if ("V3".equals(level)) {
            return sysProduct.getV3Pricing();
        } else if ("V4团队经理".equals(level)) {
            return sysProduct.getV4Pricing();
        } else {
            return sysProduct.getV5Pricing();
        }
    }

    public List<PromoteEquity> promoteEquity(List<SysProduct> sysProducts) {
        List<PromoteEquity> promoteEquitys = new ArrayList<>();
        Long userId = SecurityUtils.getUserId();
        // 2. 获取用户信息（包括级别）
        SysUser user = iSysUserService.getUserLevel(new SysUser(userId));
        for (SysProduct sysProduct : sysProducts) {
            SysUserProductPrice upp = selectSysUserProductPrice.selectSysUserProductPrice(userId, sysProduct.getProductId());
            if (upp != null && upp.getRoyalty() != null) {
                sysProduct.setRoyalty(upp.getRoyalty());
            }
            if (upp != null && upp.getBrokerage() != null) {
                sysProduct.setBrokerage(upp.getBrokerage());
            }

            BigDecimal sysFloorPrice = null, sj_royalty = new BigDecimal(0), addPrice;
            if (upp != null && upp.getCustomizedPrice() != null) {
                sysFloorPrice = upp.getCustomizedPrice();
                addPrice = upp.getCustomizedPrice();
            } else {
                addPrice = getSysUserLevelFloorPrice(sysProduct, user.getGrade());
            }
            if (user != null && StringUtils.isNotEmpty(user.getParent())) {  // 有上级
                SysUserProductPrice sjupp = selectSysUserProductPrice.selectSysUserProductPrice(Long.parseLong(user.getParent()), sysProduct.getProductId());
                if (sjupp != null && sjupp.getRoyalty() != null) {
                    addPrice = addPrice.add(sjupp.getRoyalty());
                    sj_royalty = sjupp.getRoyalty();
                } else {
                    addPrice = addPrice.add(sysProduct.getRoyalty());
                    sj_royalty = sysProduct.getRoyalty();
                }
            }
            addPrice = addPrice.add(sysProduct.getBrokerage());
            // 价格分级系统抽润
            BigDecimal chouRui = getChouRui(addPrice);
            sysProduct.setBrokerage(sysProduct.getBrokerage().subtract(chouRui));
            if (upp != null && upp.getMaxRoyalty() != null) {
                sysProduct.setMaxRoyalty(upp.getMaxRoyalty());
            }
            if (upp != null && upp.getMaxBrokerage() != null) {
                sysProduct.setMaxBrokerage(upp.getMaxBrokerage());
            }
            promoteEquitys.add(new PromoteEquity(sysProduct, sysFloorPrice, sj_royalty, chouRui));
        }
        return promoteEquitys;
    }

    public static BigDecimal getChouRui(BigDecimal addPrice) {
        float sj = addPrice.floatValue();
        BigDecimal decimal, starJf = BigDecimal.valueOf(30);
        if (sj <= 30) {
            decimal = new BigDecimal(0);
        } else if (sj <= 90) {
            decimal = addPrice.subtract(starJf).multiply(BigDecimal.valueOf(0.08));
        } else if (sj <= 120) {
            decimal = addPrice.subtract(starJf).multiply(BigDecimal.valueOf(0.09));
        } else {
            decimal = addPrice.subtract(starJf).multiply(BigDecimal.valueOf(0.1));
        }
        return decimal;
    }

    @Override
    public List<ProductEquity> productEquity(List<SysProduct> sysProducts) {
        List<ProductEquity> productEquities = new ArrayList<>();
        Long userId = SecurityUtils.getUserId();
        SysUser user = iSysUserService.getUserLevel(new SysUser(userId));
        Boolean isAgent = user != null && user.getParent() != null && !"".equals(user.getParent());
        for (SysProduct sysProduct : sysProducts) {
            BigDecimal sysFloorPrice = null, royalty;
            SysUserProductPrice upp = selectSysUserProductPrice.selectSysUserProductPrice(userId, sysProduct.getProductId());
            if (upp != null && upp.getCustomizedPrice() != null) {
                sysFloorPrice = upp.getCustomizedPrice();
            }
            if (isAgent) {                     // 存在上级
                // 2. 获取底价
                SysUserProductPrice sjupp = selectSysUserProductPrice.selectSysUserProductPrice(Long.parseLong(user.getParent()), sysProduct.getProductId());
                if (sjupp != null && sjupp.getRoyalty() != null) {
                    royalty = sjupp.getRoyalty();
                } else {
                    royalty = sysProduct.getRoyalty();
                }
            } else {
                royalty = new BigDecimal(0);
            }
            productEquities.add(new ProductEquity(sysProduct, sysFloorPrice, royalty));
        }
        return productEquities;
    }

    @Override
    public Promote calculatePriceInfo(Long userId, int productId) {
        // 1. 获取产品信息
        SysProduct sysProduct = sysProductMapper.selectSysProductByProductId(productId);
        // 2. 获取用户信息（包括级别）
        SysUser user = iSysUserService.getUserLevel(new SysUser(userId));
        // 2. 拉取用户定制的价目信息
        BigDecimal sysFloorPrice, royalty = null, floorPrice, brokerage, pricing;
        SysUserProductPrice upp = selectSysUserProductPrice.selectSysUserProductPrice(userId, productId);
        if (upp != null && upp.getCustomizedPrice() != null) {
            sysFloorPrice = upp.getCustomizedPrice();
        } else {
            sysFloorPrice = getSysUserLevelFloorPrice(sysProduct, user.getGrade());
        }
        if (user != null && StringUtils.isNotEmpty(user.getParent())) {  // 有上级
            SysUserProductPrice sjupp = selectSysUserProductPrice.selectSysUserProductPrice(Long.parseLong(user.getParent()), productId);
            if (sjupp != null && sjupp.getRoyalty() != null) {
                royalty = sjupp.getRoyalty();
            } else {
                royalty = sysProduct.getRoyalty();
            }
            floorPrice = sysFloorPrice.add(royalty);
        } else {
            floorPrice = sysFloorPrice;
        }
        if (upp != null && upp.getBrokerage() != null) {
            brokerage = upp.getBrokerage();
        } else {
            brokerage = sysProduct.getBrokerage();
        }
        pricing = floorPrice.add(brokerage);
        // 价格分级系统抽润
        BigDecimal chouRui = getChouRui(pricing);
        return new Promote(sysProduct,
                floorPrice, sysFloorPrice.add(chouRui),
                royalty, brokerage.subtract(chouRui), pricing);
    }

    /**
     * 查询产品
     *
     * @param productId 产品主键
     * @return 产品
     */
    @Override
    public SysProduct selectSysProductByProductId(int productId) {
        return sysProductMapper.selectSysProductByProductId(productId);
    }

    /**
     * 查询产品列表
     *
     * @param sysProduct 产品
     * @return 产品
     */
    @Override
    public List<SysProduct> selectSysProductList(SysProduct sysProduct) {
        return sysProductMapper.selectSysProductList(sysProduct);
    }

    /**
     * 新增产品
     *
     * @param sysProduct 产品
     * @return 结果
     */
    @Override
    public int insertSysProduct(SysProduct sysProduct) {
        sysProduct.setCreateTime(DateUtils.getNowDate());
        return sysProductMapper.insertSysProduct(sysProduct);
    }

    /**
     * 修改产品
     *
     * @param sysProduct 产品
     * @return 结果
     */
    @Override
    public int updateSysProduct(SysProduct sysProduct) {
        sysProduct.setUpdateTime(DateUtils.getNowDate());
        return sysProductMapper.updateSysProduct(sysProduct);
    }

}
